package recordswipetime

import (
	"go-interview-forum/model/recordswipetime"
	"time"
)

func CreateRecordSwipeTime(userId int, data map[string]interface{}) error {
	recordSwipeTime := recordswipetime.RecordSwipeTime{UserId: userId}
	if data["from"].(string) == "add" {
		recordSwipeTime.EndTime = time.Now()
		recordSwipeTime.StartingTime = time.UnixMilli(int64(data["time"].(float64)))
		return recordSwipeTime.CreateRecordSwipeTime()
	}

	err := recordSwipeTime.GetRecordSwipeTimeInfo()
	if err != nil {
		return err
	}

	endTime := time.UnixMilli(int64(data["time"].(float64)))
	//计算刷题总时长
	recordSwipeTime.SumTime = uint64(endTime.Unix() - recordSwipeTime.StartingTime.Unix())

	recordSwipeTime.EndTime = endTime

	return recordSwipeTime.UpdateRecordSwipeTimeInfo()
}
